/**
 * 
 */
package com.base.myproject.client.ui.celleditor;

import com.base.myproject.client.tools.Debug;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.google.gwt.user.client.Element;

/**
 * @author zgw 2009-10-9 上午10:55:16 javazgw@gmail.com
 */
public class GridEditor extends CellEditor implements GridCellRenderer {
	BaseGridEditor basegrideditor;
	Object tempvalue;

	String render_source_property,render_target_property;
	public GridEditor(BaseGridEditor field)
	{
		this(field,null,null);
	}
	public GridEditor(BaseGridEditor field,String render_source_property,String render_target_property) {
		super(field);
		basegrideditor = field;
		this.render_source_property = render_source_property;
		this.render_target_property = render_target_property;
	}

	@Override
	public Object render(ModelData model, String property, ColumnData config,
			int rowIndex, int colIndex, ListStore store, Grid grid) {
//		if(render_target_property!=null)
		if(!(basegrideditor.getBasestore()==null ||render_target_property==null ))
		{
		ModelData m =basegrideditor.getBasestore().findModel(render_source_property, model.get(property));
		if(m!=null)
		{
			Debug.printf(m.get(render_target_property), 4);
			return m.get(render_target_property);
		}

	
		Debug.printf(model.get(property), 4);
		Debug.printf("------------------------end ", 4);
		}
		return model.get(property);
	}

	/**
	 * 提交前
	 */
	public Object preProcessValue(Object value) {
	//	System.out.println("preProcessValue...........");
		if (value == null) {
			return value;
		}

		// return combobox.findModel(value.toString(), this.htvalue, "");
		// return combobox.getValue();
		tempvalue = value;// basegrideditor.getValue();
		//System.out.println("tempvalue =====" + tempvalue);
		return tempvalue;
		// return "zzz";
	}

	/**
	 * 最终提交
	 */
	public Object postProcessValue(Object value) {
	//	System.out.println("postProcessValue................");

		if (value == null) {

			return value;
		}

//		if (!tempvalue.equals(value)) {
//			System.out.println("tempvalue1=" + tempvalue + "  value=" + value);
//			// post();
//		}
	//	System.out.println("basegrideditor.getValue() = "
	//			+ basegrideditor.getValue());
		return basegrideditor.getValue();
		// return "zzzz";
	}

	/**
	 * 光标移开时候
	 */
	protected void onBlur(FieldEvent fe) {
		//System.out.println("onBlur..........."+basegrideditor.isediting);
		if (!basegrideditor.isediting) {
			
			if (!isAllowBlur() && !basegrideditor.isshow()) {
				//System.out.println("blur 1");
				completeEdit();
				//System.out.println("blur 2");
			}
		}
	}

	public void startEdit(Element el, Object value) {
		//System.out.println("startedit...........");
		super.startEdit(el, value);
	//	basegrideditor.isediting = true;
	}

	public void completeEdit() {
		//System.out.println("completeEdit...........");
		if (basegrideditor.isediting) {
			return ;
		}
		completeEdit(false);
	}
	
	public void post() {
		System.out.println("post");
	}
}
